<?php

use Phalcon\Mvc\Controller;
use Phalcon\Config\Adapter\Ini as ConfigIni;

class ControllerBase extends Controller
{

    protected function initialize()
    {
        $this->tag->appendTitle('| Priceasia');

        $config = new ConfigIni(realpath('..') . '/app/config/config.ini');
        $this->view->setVar('config', $config);
        
        $frontCache = new Phalcon\Cache\Frontend\Data(array(
            "lifetime" => 172800
        ));
        $cache = new Phalcon\Cache\Backend\File($frontCache, array(
            "cacheDir" => "../app/cache/"
        ));

        $cacheKey = 'menu.cache';
        $menu_cache    = $cache->get($cacheKey);
        if ($menu_cache === null) {

            $menu_cache = Menu::find('dm_cha_ids = "584" OR dm_cha_ids = "575"');

            // Store it in the cache
            $cache->save($cacheKey, $menu_cache);
        }

        //$rootMenuItems = Menu::find('dm_cha_ids = "584" OR dm_cha_ids = "575"');
        $this->view->setVar('root_menu_items', $menu_cache);
    }

    protected function forward($uri)
    {
        $uriParts = explode('/', $uri);
        $params = array_slice($uriParts, 2);
    	return $this->dispatcher->forward(
    		array(
    			'controller' => $uriParts[0],
    			'action' => $uriParts[1],
                'params' => $params
    		)
    	);
    }
}